layui.use('table', function(){
    var table = layui.table,
        $ = layui.jquery,
        layer = parent.layer === undefined ? layui.layer : parent.layer;

    function getParams(){
        return {
            elem: '#apiList'
                ,height: 500
            ,toolbar:"<div class=\"layui-btn-container\">\n" +
        "    <button class=\"layui-btn layui-btn-normal\" lay-event=\"add\">添加Api</button>\n" +
        "    <button class=\"layui-btn layui-btn-normal\" lay-event=\"update\">编辑</button>\n" +
        "    <button class=\"layui-btn \" lay-event=\"updateStatus1\">设置有效</button>\n" +
        "    <button class=\"layui-btn layui-btn-danger\" lay-event=\"updateStatus0\">设置无效</button>\n" +
        "    <button class=\"layui-btn layui-btn-normal\" lay-event=\"refresh\">更新缓存</button>\n" +
        "  </div>"
            ,url: '/apiInfo/list'
            ,page: true //开启分页
            ,cols: [[ //表头
            {field:"checkbox",type:"checkbox", title: '', width:"5%",fixed: 'left'}
            ,{field: 'id', title: 'ID', width:"5%",fixed: 'left'}
            ,{field: 'apiPath', title: 'API路径', width:"15%"}
            ,{field: 'orgHost', title: '服务器地址', width:"15%"}
            ,{field: 'orgUrl', title: '服务器路径', width:"15%"}
            ,{field: 'createTime', title: '创建时间', width: "20%",templet:function (data) {
                    return new Date(data.createTime).toLocaleString();
                }}
            ,{field: 'createUser', title: '创建人', width: "15%"}
            ,{field: 'status', title: '状态', width: "10%",templet:function (data) {
                    return data.status==1?"有效":"无效";
                }}
            /*,{field: 'operation', title: '操作', width: "10%",
                templet:function (data) {
                    return '<div><button data-id="'+data.id+'" class="layui-btn layui-btn-xs layui-btn-normal edit" >编辑</button>' +
                    '<button data-id="'+data.id+'" class="layui-btn layui-btn-xs layui-btn-danger delete" >删除</button></div>'
                }
            }*/
            ]]
            ,parseData: function(res) { //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                // "count": res.total, //解析数据长度
                "data": res.result //解析数据列表
                };
            }
        };
    }
    table.render(getParams());
    table.on('toolbar(apiTable)', function(obj){
        var checkStatus = table.checkStatus(obj.config.id);
        switch(obj.event){
            case 'add':
                var index = layui.layer.open({
                    title : "新增Api",
                    type : 2,
                    content : "../apiInfo/addApi",
                    success : function(layero, index){
                        setTimeout(function(){
                            layui.layer.tips('点击此处返回参数列表', '.layui-layer-setwin .layui-layer-close', {
                                tips: 3
                            });
                        },300)
                    }
                })
                layui.layer.full(index);
                break;
            case 'update':
                if(checkStatus.data.length!=1){
                    layer.msg("请选择一条记录进行编辑!");
                }else{
                    var apiId = checkStatus.data[0].id;
                    var index = layui.layer.open({
                        title : "编辑Api",
                        type : 2,
                        content : "../apiInfo/addApi?apiId="+apiId,
                        success : function(layero, index){
                            setTimeout(function(){
                                layui.layer.tips('点击此处返回参数列表', '.layui-layer-setwin .layui-layer-close', {
                                    tips: 3
                                });
                            },300)
                        }
                    })
                    layui.layer.full(index);
                }
                break;
            case 'updateStatus1':
                var length = checkStatus.data.length;
                if(length==0){
                    layer.msg("请选择至少一条记录进行状态修改!");
                    break;
                }
                var apiIds = checkStatus.data[0].id;
                for(var i = 1 ; i<length;i++){
                    apiIds+= ","+checkStatus.data[i].id;
                }
                $.ajax({
                    url : "../apiInfo/updateStatus",
                    type : "post",
                    data : {
                        apiIds:apiIds,
                        status:1
                    },
                    dataType : "json",
                    success : function(data){
                        if(!data.success){
                            layer.msg("更新失败！");
                        }else{
                            layer.msg("更新成功！");
                            table.render(getParams());
                        }
                    }
                });
                break;
            case 'updateStatus0':
                var length = checkStatus.data.length;
                if(length==0){
                    layer.msg("请选择至少一条记录进行状态修改!");
                    break;
                }
                var apiIds = checkStatus.data[0].id;
                for(var i = 1 ; i<length;i++){
                    apiIds+= ","+checkStatus.data[i].id;
                }
                console.log(apiIds)
                $.ajax({
                    url : "../apiInfo/updateStatus",
                    type : "post",
                    data : {
                        apiIds:apiIds,
                        status:0
                    },
                    dataType : "json",
                    success : function(data){
                        if(!data.success){
                            layer.msg("更新失败！");
                        }else{
                            layer.msg("更新成功！");
                            table.render(getParams());
                        }
                    }
                });
                break;
            case 'refresh':
                $.ajax({
                    url : "../apiInfo/refresh",
                    type : "post",
                    dataType : "json",
                    success : function(data){
                        if(!data.success){
                            layer.msg("刷新缓存失败！请联系管理员！");
                        }else{
                            layer.msg("刷新缓存成功！您现在可以使用最新的API了");
                        }
                    }
                });
                break;
        }
    });
});